package cn.iocoder.yudao.module.wine.service.complaincomment;

import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.wine.controller.app.complaincomment.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.complaincomment.ComplainCommentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.complaincomment.ComplainCommentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 投诉信息评价 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class ComplainCommentServiceImpl implements ComplainCommentService {

    @Resource
    private ComplainCommentMapper complainCommentMapper;

    @Override
    public Long createComplainComment(AppComplainCommentSaveReqVO createReqVO) {
        // 插入
        ComplainCommentDO complainComment = BeanUtils.toBean(createReqVO, ComplainCommentDO.class);
        complainComment.setUserId(SecurityFrameworkUtils.getLoginUserId());
        complainCommentMapper.insert(complainComment);
        // 返回
        return complainComment.getId();
    }

    @Override
    public void updateComplainComment(AppComplainCommentSaveReqVO updateReqVO) {
        // 校验存在
        validateComplainCommentExists(updateReqVO.getId());
        // 更新
        ComplainCommentDO updateObj = BeanUtils.toBean(updateReqVO, ComplainCommentDO.class);
        complainCommentMapper.updateById(updateObj);
    }

    @Override
    public void deleteComplainComment(Long id) {
        // 校验存在
        ComplainCommentDO complainCommentDO = validateComplainCommentExists(id);
        if (!SecurityFrameworkUtils.getLoginUserId().equals(complainCommentDO.getUserId())){
            throw exception(HAVE_NO_RIGHT_COMMENT);
        }
        // 删除
        complainCommentMapper.deleteById(id);
    }

    private ComplainCommentDO validateComplainCommentExists(Long id) {
        ComplainCommentDO complainCommentDO = complainCommentMapper.selectById(id);
        if (complainCommentDO == null) {
            throw exception(INFO_NOT_EXISTS);
        }
        return complainCommentDO;
    }

    @Override
    public PageResult<ComplainCommentDO> getComplainCommentPage(AppComplainCommentPageReqVO pageReqVO) {
        return complainCommentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AppComplainCommentRespVO> getComplainCommentList(Long complainId) {
        return complainCommentMapper.getComplainCommentList(complainId);

    }

    @Override
    public AppComplainCommentRespVO getComplainCommentById(Long commentId) {

        return complainCommentMapper.selectJoinOne(AppComplainCommentRespVO.class,new MPJLambdaWrapperX<ComplainCommentDO>()
                .eq(ComplainCommentDO::getId,commentId)
                .leftJoin(MemberUserDO.class,MemberUserDO::getId,ComplainCommentDO::getUserId)
                .selectAll(ComplainCommentDO.class)
                .selectAs(MemberUserDO::getNickname,AppComplainCommentRespVO::getNickname));
    }

}
